# -*- coding: utf-8 -*-
# @Time    : 2022/3/18 15:44

import json
import inspect
from Common.mysql import app
from flask import jsonify, request
from Common.yaml_method import YamlMethod
from src.ApiTest.Core.sys_function import SysFunction
from src.User.token import token_verify, token_user_id, before_request
from src.ApiTest.ConfigurationData.Function.add_data import AddData
from src.ApiTest.ConfigurationData.Function.data_list import DataList
from src.ApiTest.ConfigurationData.Function.update_data import UpdateData
from src.ApiTest.ConfigurationData.Function.delete_data import DeleteData

code = YamlMethod().read_data('code.yaml')['code']


@app.route('/data/add', methods=['POST'])
@token_verify
def add_data(create_user):
    """
    添加测试数据信息接口
    :param create_user: 操作用户名
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    data_name = data['data_name']
    project_data = data['projectName']
    if '/' in project_data:
        project_data = data['projectName'].split('/')
        project_name = project_data[0]
        project_id = project_data[1]
    else:
        project_name = project_data
        project_id = data['projectId']
    data_type = data['data_type']
    value = data['data_value']
    key = data['data_key']

    # 校验参数
    if not all([project_id, project_name, data_name, value, key, create_user]):
        return jsonify(code=code[7], msg="信息不完整")

    # 提交注册信息
    res = AddData().add_data(project_id, project_name, data_name, str(data_type), key, value, create_user)
    return jsonify(res)


@app.route('/data/list', methods=['POST'])
@token_user_id
def data_list(user_id):
    """
    获取测试数据列表接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    page = data['page']
    limit = data['limit']
    data_name = data['data_name']
    project_name = data['projectName']
    if '/' in str(project_name):
        project_name = project_name.split('/')[0]

    res = DataList().data_list(page, limit, data_name, user_id, project_name)
    return res


@app.route('/data/name', methods=['POST'])
@before_request
def get_data_name():
    """
    获取测试数据名称接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    page = data['current']
    limit = data['size']

    res = DataList().data_name(page, limit)
    return res


@app.route('/data/update', methods=['POST'])
@token_verify
def update_data(update_user):
    """
    更新测试数据信息接口
    :param update_user: 操作用户名
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    data_id = data['id']
    project_data = data['projectName']
    if '/' in project_data:
        project_data = data['projectName'].split('/')
        project_name = project_data[0]
        project_id = project_data[1]
    else:
        project_name = project_data
        project_id = data['projectId']
    data_name = data['data_name']
    data_type = data['data_type']
    value = data['data_value']
    key = data['data_key']

    # 校验参数
    if not all([project_id, project_name, str(data_id), data_name, str(data_type), value, key, update_user]):
        return jsonify(code=code[7], msg="信息不完整")

    res = UpdateData().update_data(project_id, project_name, data_id, data_name, data_type, key, value, update_user)
    return res


@app.route('/data/delete', methods=['POST'])
@before_request
def delete_data():
    """
    删除测试数据信息接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    data_name = data['data_name']

    res = DeleteData().delete_data(data_name)
    return res


@app.route('/data/function_name', methods=['POST'])
@before_request
def function_name():
    """
    获取公共方法函数名称接口
    :return:
    """
    function_names = [name for name, _ in inspect.getmembers(SysFunction, inspect.isfunction)]

    res = {
        'code': code[0],
        'data': function_names
    }

    return res
